﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardInChina.Bussiness.Frontend.Models;
using CardInChina.Data.Entity.Interface;
using CardInChina.Data.Entity.Entity;
using System.Collections;

namespace CardInChina.Bussiness.Frontend.Interface
{
    public class OrderBusinessService
    {

        private CardService cs = new CardService();
        private OrderService orderService = new OrderService();

        public string CreateOrder(InputOrderInfo inputOrder,CustomerInfo customerInfo)
        {
            if (inputOrder == null) throw new ArgumentNullException("inputOrder");
            if (customerInfo == null) throw new ArgumentNullException("customerInfo");
            if(customerInfo.CustomerID == 0) throw new ArgumentNullException("customerInfo.CustomerID");
            //if(customerInfo.IsActived)    old system does not verify this
            //get cardinfo
            Card card = cs.GetCardEntityByCardID(inputOrder.OrderCardID);
            if (card == null) throw new NullReferenceException(string.Format("Card with Id {} was not found!", inputOrder.OrderCardID));
            //build order
            Order order = new Order();
            order.CardAmount = inputOrder.Amount;
            order.CardID = card.CardID;
            order.CustomerID = customerInfo.CustomerID;
            order.GoodsType = card.GoodsType;
            order.TotalPrice = customerInfo.IsVIP ? card.VIPPrice * inputOrder.Amount : card.Price * inputOrder.Amount;
            order.CustomerMemo = inputOrder.CustomerMemo;
            order.OrderCreateTime = DateTime.Now; 
            order.LastModifiedTime = DateTime.Now;
            //order.PaidCurrency = string.Empty;
           
            //TODO
            //order.IP
            //order.IPCountry

            //build order_detail
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.Amount = inputOrder.Amount;
            orderDetail.GoodsType = card.GoodsType;
            orderDetail.CardID = card.CardID;


            //build charge_detail
            ChargeDetail chargeDetail = null;
            if (card.GoodsType == 1)
            {
                chargeDetail = new ChargeDetail();
                if (inputOrder.GameSelected == 1)
                {
                    Card card2 = card.Load<ICollection<ChargeOption>>();
                    ChargeOption co = card2.OfChargeOptions.First(charge => charge.ChargeOptionID.ToString() == inputOrder.GameName);
                    chargeDetail.ChargeMethod = co.OptionName;
                    chargeDetail.ServerID = co.OptionValue ?? string.Empty;
                }
                else
                {
                    chargeDetail.ServerID = string.Empty;
                    chargeDetail.ChargeMethod = inputOrder.GameName;
                }
                chargeDetail.GameAccount = inputOrder.GameID;
                chargeDetail.ServerArea = inputOrder.ServerArea ?? string.Empty;
                chargeDetail.ServerName = inputOrder.ServerName;
                //chargeDetail.SentStatus = 0;
                //chargeDetail.Sender = string.Empty;
            }

            order.OfOrderDetails.Add(orderDetail);
            order.OfChargeDetails.Add(chargeDetail);
            orderDetail.OfChargeDetails.Add(chargeDetail);

            return orderService.InsertOneOrder(order);
        }

        public Order GetOrderByOrderIndex(string index)
        {
            return orderService.GetOrderByOrderID(index);
        }
    }
}
